load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("@fbcode_macros//build_defs:cpp_unittest.bzl", "cpp_unittest")
load("@fbsource//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
load("@fbsource//tools/build_defs:rust_library.bzl", "rust_library")

oncall("sapling")

fb_native.constraint_setting(
    name = "edenfs-cas",
)

fb_native.constraint_value(
    name = "edenfs-include-cas",
    constraint_setting = ":edenfs-cas",
    visibility = ["PUBLIC"],
)

cpp_library(
    name = "sapling_backingstore_error",
    headers = {
        "SaplingBackingStoreError.h": "include/SaplingBackingStoreError.h",
    },
    exported_deps = ["fbsource//third-party/rust:cxx-core"],
)

cpp_library(
    name = "sapling_native_backingstore",
    srcs = glob(["src/**/*.cpp"]),
    headers = glob(["include/**/*.h"]),
    undefined_symbols = True,
    deps = [
        "//folly:try",
        "//folly/io:iobuf",
    ],
    exported_deps = [
        "fbsource//third-party/rust:cxx-core",
        ":backingstore@header",  # @manual
        "//eden/common/utils:case_sensitivity",
        "//eden/common/utils:path",
        "//eden/fs/config:config",
        "//eden/fs/model:model",
        "//eden/fs/model:model-fwd",
        "//eden/fs/store/hg:hg_proxy_hash",
        "//folly:range",
        "//folly/container:fbvector",
        "//folly/futures:core",
    ],
)

rust_library(
    name = "backingstore",
    srcs = glob(["src/**/*.rs"]),
    autocargo = {
        "cargo_target_config": {"crate_type": [
            "staticlib",
            "lib",
        ]},
        "cargo_toml_config": {
            "dependencies_override": {"dependencies": {
                "sapling-constructors": {"default-features": False},
                "sapling-indexedlog": {"features": ["configurable"]},
            }},
            "features": {"fb": ["sapling-configloader/fb"]},
            "lib": {"name": "backingstore"},
            "package": {
                "authors": ["Meta Source Control Team <sourcecontrol-dev@meta.com>"],
                "homepage": "https://sapling-scm.com/",
                "license": "MIT",
                "name": "sapling-backingstore",
                "repository": "https://github.com/facebook/sapling",
            },
        },
    },
    cpp_deps = [
        ":sapling_native_backingstore",
        "//eden/fs/config:config",
        "//eden/fs/model:model",
        "//eden/fs/store:context",
    ],
    crate_root = "src/lib.rs",
    cxx_bridge = "src/ffi.rs",
    features = ["scuba"] + select({
        "DEFAULT": [],
        "fbcode//eden/scm/lib/backingstore:edenfs-include-cas": select({
            "DEFAULT": [],
            "ovr_config//os:linux": ["cas"],
        }),
    }),
    # thin-cas-client should be in deps but Autocargo does not support selects
    # and will ignore fields with them. Since we don't have any OSS named_deps,
    # we can put the select here and let Autocargo ignore it safely, while
    # still getting the dep.
    named_deps = select({
        "DEFAULT": {},
        "fbcode//eden/scm/lib/backingstore:edenfs-include-cas": select({
            "DEFAULT": {},
            "ovr_config//os:linux": {"thin_cas_client": "//eden/scm/lib/cas-client/thin-client:thin-cas-client"},
        }),
    }),
    test_deps = [
        "fbsource//third-party/rust:http",
        "fbsource//third-party/rust:rand_chacha",
        "fbsource//third-party/rust:url",
    ],
    deps = [
        "fbsource//third-party/rust:anyhow",
        "fbsource//third-party/rust:arc-swap",
        "fbsource//third-party/rust:cxx",
        "fbsource//third-party/rust:env_logger",
        "fbsource//third-party/rust:flume",
        "fbsource//third-party/rust:log",
        "fbsource//third-party/rust:parking_lot",
        "fbsource//third-party/rust:smallvec",
        "fbsource//third-party/rust:tracing",
        "fbsource//third-party/rust:tracing-subscriber",
        "//common/rust/folly:iobuf",
        "//eden/fs/rust/facebook/edenfs-telemetry:edenfs-telemetry",
        "//eden/scm/lib/blob:blob",
        "//eden/scm/lib/config/loader:configloader",
        "//eden/scm/lib/constructors:constructors",
        "//eden/scm/lib/eagerepo:eagerepo",
        "//eden/scm/lib/edenapi:edenapi",
        "//eden/scm/lib/http-client:http-client",
        "//eden/scm/lib/identity:identity",
        "//eden/scm/lib/indexedlog:indexedlog",
        "//eden/scm/lib/manifest:manifest",
        "//eden/scm/lib/manifest-tree:manifest-tree",
        "//eden/scm/lib/metrics:metrics",
        "//eden/scm/lib/pathmatcher:pathmatcher",
        "//eden/scm/lib/progress/model:progress-model",
        "//eden/scm/lib/repo:repo",
        "//eden/scm/lib/revisionstore:revisionstore",
        "//eden/scm/lib/storemodel:storemodel",
        "//eden/scm/lib/types:types",
        "//eden/scm/lib/util/testutil:testutil",
        "//eden/scm/lib/walkdetector:walkdetector",
    ],
)

cpp_unittest(
    name = "tree_builder_test",
    srcs = [
        "test/TreeBuilderTest.cpp",
    ],
    supports_static_listing = False,
    deps = [
        "fbsource//third-party/googletest:gtest",
        ":sapling_native_backingstore",
        "//eden/common/utils:path",
        "//eden/fs/config:config",
        "//eden/fs/model:model",
    ],
)
